﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace VIT.Provider
{
    public class Position : Position_Base
    {
        #region Variable
        string user = HttpContext.Current.Session["userName"].ToString();
        #endregion

        public Position(string id = "",
                        string name = "",
                        string description = "")    
        {
            this.Id = id;
            this.Name = name;
            this.Description = description;
        }

        #region Methods
        public DataTable getAll(string SortColumn = "Name")
        {
            if (SortColumn == "") SortColumn = "Name";
            string sqlCat = "select * from Position order by " + SortColumn;
            return ExecuteDataset(false, sqlCat).Tables[0];
        }

        public bool Delete(string id)
        {
            string strQuery = "Delete from Position where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", id);

            return ExecuteNonQuery(false, strQuery, Id) > 0;
        }

        public void GetSingle()
        {
            if (Exist())
            {
                string strQuery = "select * from Position where Id=@Id";
                SqlParameter Id = new SqlParameter("@Id", this.Id);
                DataTable dt = ExecuteDataset(false, strQuery, Id).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    this.Id = dt.Rows[0]["Id"].ToString();
                    this.Name = dt.Rows[0]["Name"].ToString();
                    this.Description = dt.Rows[0]["Description"].ToString();
                }
            }
            else this.Id = "";
        }

        public bool Exist()
        {
            if (this.Id == "") return false;
            string strQuery = "select Id from Position where Id=@Id";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            return ExecuteScalar(false, strQuery, Id) != null;
        }

        public bool Save()
        {
            if (Exist())
            {
                return Update();
            }
            else
            {
                return Insert();
            }
        }

        public bool Insert()
        {
            string strQuery = "insert into Position values (@Id, @Name, @Description, @Now, @Now, @User, @User)";

            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, Name, Description, User, Now) > 0;            
        }

        public bool Update()
        {
            string strQuery = "update Position set Name=@Name, Description=@Description, ModifyDate=@Now, ModifyByUser=@User where Id=@Id";

            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter Name = new SqlParameter("@Name", this.Name);
            SqlParameter Description = new SqlParameter("@Description", this.Description);
            SqlParameter User = new SqlParameter("@User", user);
            SqlParameter Now = new SqlParameter("@Now", DateTime.Now);

            return ExecuteNonQuery(false, strQuery, Id, Name, Description, User, Now) > 0;            
        }
        #endregion

    }
}
